home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / lib / OIOfd.h < prev    next >
C/C++ Source or Header  |  1990-05-19  |  6KB  |  232 lines

  1. #ifndef    OIOFD_H
  2. #define    OIOFD_H
  3.  
  4. /*$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/OIOfd.h,v 3.0 90/05/20 00:20:28 kgorlen Rel $*/
  5.  
  6. /* IOfd.h -- declarations for NIHCL file Object I/O classes
  7.  
  8.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  9.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  10.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  11.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  12.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  13.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  14.  
  15. Author:
  16.     K. E. Gorlen
  17.     Computer Systems Laboratory, DCRT
  18.     National Institutes of Health
  19.     Bethesda, MD 20892
  20.     May, 1989
  21.  
  22. Modification History:
  23.  
  24. $Log:    OIOfd.h,v $
  25.  * Revision 3.0  90/05/20  00:20:28  kgorlen
  26.  * Release for 1st edition.
  27.  * 
  28. */
  29. #include "Object.h"
  30. #include <string.h>
  31. #include <osfcn.h>
  32.  
  33. class OIOifd : public NIHCL {
  34.     int fd;            // file descriptor
  35. protected:
  36.     virtual Object* readObject(const Class&);
  37.     friend Object* Class::readFrom(OIOifd&) const;
  38. public:
  39.     OIOifd(int filedesc)    { fd = filedesc; }
  40.     operator int()        { return fd; }
  41.     OIOifd& operator>>(char& val) {
  42.         get((char*)&val,sizeof(val));
  43.         return *this;
  44.     }
  45.     OIOifd& operator>>(unsigned char& val) {
  46.         get((char*)&val,sizeof(val));
  47.         return *this;
  48.     }
  49.     OIOifd& operator>>(short& val) {
  50.         get((char*)&val,sizeof(val));
  51.         return *this;
  52.     }
  53.     OIOifd& operator>>(unsigned short& val) {
  54.         get((char*)&val,sizeof(val));
  55.         return *this;
  56.     }
  57.     OIOifd& operator>>(int& val) {
  58.         get((char*)&val,sizeof(val));
  59.         return *this;
  60.     }
  61.     OIOifd& operator>>(unsigned int& val) {
  62.         get((char*)&val,sizeof(val));
  63.         return *this;
  64.     }
  65.     OIOifd& operator>>(long& val) {
  66.         get((char*)&val,sizeof(val));
  67.         return *this;
  68.     }
  69.     OIOifd& operator>>(unsigned long& val) {
  70.         get((char*)&val,sizeof(val));
  71.         return *this;
  72.     }
  73.     OIOifd& operator>>(float& val) {
  74.         get((char*)&val,sizeof(val));
  75.         return *this;
  76.     }
  77.     OIOifd& operator>>(double& val) {
  78.         get((char*)&val,sizeof(val));
  79.         return *this;
  80.     }
  81.     int get()            { char c; get(c); return c; }
  82.     OIOifd&    get(char& c)        { get(&c,1); return *this; }
  83.     OIOifd&    get(unsigned char& c)    { get(&c,1); return *this; }
  84.     OIOifd& get(char*, unsigned size);
  85.     OIOifd& get(unsigned char* val, unsigned size) {
  86.         get((char*)val,size*sizeof(*val));
  87.         return *this;
  88.     }
  89.     OIOifd& get(short* val, unsigned size) {
  90.         get((char*)val,size*sizeof(*val));
  91.         return *this;
  92.     }
  93.     OIOifd& get(unsigned short* val, unsigned size) {
  94.         get((char*)val,size*sizeof(*val));
  95.         return *this;
  96.     }
  97.     OIOifd& get(int* val, unsigned size) {
  98.         get((char*)val,size*sizeof(*val));
  99.         return *this;
  100.     }
  101.     OIOifd& get(unsigned int* val, unsigned size) {
  102.         get((char*)val,size*sizeof(*val));
  103.         return *this;
  104.     }
  105.     OIOifd& get(long* val,unsigned size) {
  106.         get((char*)val,size*sizeof(*val));
  107.         return *this;
  108.     }
  109.     OIOifd& get(unsigned long* val,unsigned size) {
  110.         get((char*)val,size*sizeof(*val));
  111.         return *this;
  112.     }
  113.     OIOifd& get(float* val,unsigned size) {
  114.         get((char*)val,size*sizeof(*val));
  115.         return *this;
  116.     }
  117.     OIOifd& get(double* val,unsigned size) {
  118.         get((char*)val,size*sizeof(*val));
  119.         return *this;
  120.     }
  121.     OIOifd& getCString(char*, unsigned maxlen);
  122. };
  123.  
  124. class OIOofd : public NIHCL {
  125.     int fd;            // file descriptor
  126.     void writeErr();
  127. protected:
  128.     virtual void storeObject(const Object&);
  129.     friend void Object::storeOn(OIOofd&) const;
  130. public:
  131.     OIOofd(int filedesc)    { fd = filedesc; }
  132.     operator int()        { return fd; }
  133.     OIOofd& operator<<(const char* val) {
  134.         *this << (unsigned)strlen(val);
  135.         put(val,strlen(val));
  136.         return *this;
  137.     }
  138.     OIOofd& operator<<(char val) {
  139.         put((char*)&val,sizeof(val));
  140.         return *this;
  141.     }
  142.     OIOofd& operator<<(unsigned char val) {
  143.         put((char*)&val,sizeof(val));
  144.         return *this;
  145.     }
  146.     OIOofd& operator<<(short val) {
  147.         put((char*)&val,sizeof(val));
  148.         return *this;
  149.     }
  150.     OIOofd& operator<<(unsigned short val) {
  151.         put((char*)&val,sizeof(val));
  152.         return *this;
  153.     }
  154.     OIOofd& operator<<(int val) {
  155.         put((char*)&val,sizeof(val));
  156.         return *this;
  157.     }
  158.     OIOofd& operator<<(unsigned int val) {
  159.         put((char*)&val,sizeof(val));
  160.         return *this;
  161.     }
  162.     OIOofd& operator<<(long val) {
  163.         put((char*)&val,sizeof(val));
  164.         return *this;
  165.     }
  166.     OIOofd& operator<<(unsigned long val) {
  167.         put((char*)&val,sizeof(val));
  168.         return *this;
  169.     }
  170.     OIOofd& operator<<(float val) {
  171.         put((char*)&val,sizeof(float));
  172.         return *this;
  173.     }
  174.     OIOofd& operator<<(double val) {
  175.         put((char*)&val,sizeof(double));
  176.         return *this;
  177.     }
  178.     OIOofd& put(const char* val,unsigned size) {
  179.         write((const char*)val,size*sizeof(*val));
  180.         return *this;
  181.     }
  182.     OIOofd& put(const unsigned char* val,unsigned size) {
  183.         return put((const char*)val, size);
  184.     }
  185.     OIOofd& put(const short* val,unsigned size) {
  186.         write((const char*)val,size*sizeof(*val));
  187.         return *this;
  188.     }
  189.     OIOofd& put(const unsigned short* val,unsigned size) {
  190.         write((const char*)val,size*sizeof(*val));
  191.         return *this;
  192.     }
  193.     OIOofd& put(const int* val,unsigned size) {
  194.         write((const char*)val,size*sizeof(*val));
  195.         return *this;
  196.     }
  197.     OIOofd& put(const unsigned int* val,unsigned size) {
  198.         write((const char*)val,size*sizeof(*val));
  199.         return *this;
  200.     }
  201.     OIOofd& put(const long* val,unsigned size) {
  202.         write((const char*)val,size*sizeof(*val));
  203.         return *this;
  204.     }
  205.     OIOofd& put(const unsigned long* val,unsigned size) {
  206.         write((const char*)val,size*sizeof(*val));
  207.         return *this;
  208.     }
  209.     OIOofd& put(const float* val,unsigned size) {
  210.         write((const char*)val,size*sizeof(*val));
  211.         return *this;
  212.     }
  213.     OIOofd& put(const double* val,unsigned size) {
  214.         write((const char*)val,size*sizeof(*val));
  215.         return *this;
  216.     }
  217.     OIOofd& putCString(const char*);
  218.     OIOofd& write(const char* buf,unsigned nbyte) {
  219.         if (::write(fd,buf,nbyte) < 0) writeErr();
  220.         return *this;
  221.     }
  222. public:                // type definitions
  223.     enum oioRecordTy {        // binary object I/O record type codes
  224.         storeOnClassRef,    // class reference and object stored
  225.         storeOnClass,        // class and object stored
  226.         storeOnObjectRef    // object reference stored
  227.     };
  228.  
  229. };
  230.  
  231. #endif /* OIOFD_H */
  232.